package com.bdqn.mzc.utils;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.bdqn.mzc.config.AlipayConfig;
import com.bdqn.mzc.model.dto.PayDto;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

public class AliPay {


    public static void pay(PayDto payDto,HttpServletResponse response)throws Exception{
        DefaultAlipayClient defaultAlipayClient= new DefaultAlipayClient(AlipayConfig.gatewayUrl,AlipayConfig.app_id,AlipayConfig.merchant_private_key,"json",AlipayConfig.charset,AlipayConfig.alipay_public_key,AlipayConfig.sign_type);
        AlipayTradePagePayRequest alipayTradePagePayRequest=new AlipayTradePagePayRequest();
        Map<String,Object> map=new HashMap<>();
        map.put("out_trade_no",payDto.getNo());
        map.put("product_code","FAST_INSTANT_TRADE_PAY");
        map.put("total_amount",payDto.getPrice());
        map.put("subject",payDto.getName());
        String o= JSONObject.toJSONString(map);
        alipayTradePagePayRequest.setBizContent(o);
        alipayTradePagePayRequest.setReturnUrl(AlipayConfig.return_url);
        String body= defaultAlipayClient.pageExecute(alipayTradePagePayRequest).getBody();
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(body);
        response.getWriter().flush();
        response.getWriter().close();
    }

}
